import { createApp } from "vue";
import "./style.css";
import App from "./App.vue";

// 路由和状态管理
import router from "./router";
import { pinia } from "./stores";

// 全局事件总线
import mitt from "mitt";

// Vuetify
import { vuetify } from "./plugins/vuetify";

router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0);
  next();
});

const bus = mitt();
const app = createApp(App);

app.use(router);
app.use(pinia); // 先注册Pinia
app.use(vuetify);

app.config.globalProperties.$bus = bus; // 全局挂载事件总线

// 现在注册和使用Pinia store
import { useUserStore } from "@/stores/authStores";
const userStore = useUserStore();
const userInfo = JSON.parse(localStorage.getItem("userInfo"));
if (userInfo) {
  const { email, username, id } = userInfo;
  userStore.refreshPage(email, username, id);
}

app.mount("#app");
